#include "func.h"

void read(link *head)
{
    FILE *fp; //打开一个文件
    if ((fp = fopen("data.txt", "r")) == NULL)
    {
        perror("文件打开失败!");
        return;
    }
    con tmp;
    memset(&tmp, 0, sizeof(tmp));

    while (1)
    {
        if (fread(&tmp, sizeof(con), 1, fp) > 0)
        {
            while (head->next != NULL)
            {
                head = head->next;
            }
            //创建节点将读取到底信息保存到链表
            link *node = (link *)malloc(sizeof(link));
            memset(node, 0, sizeof(link));
            node->next = NULL;

            (node->p).id = tmp.id;
            my_strcpy((node->p).name, tmp.name);
            printf("%s\n", (node->p).name);
            (node->p).age = tmp.age;
            my_strcpy((node->p).tel, tmp.tel);
            my_strcpy((node->p).addr, tmp.addr);
            //链接
            head->next = node;
            node->prev = head;         
        }
        else
        {
            break;
        }
    }
    // printf("文件读取成功!\n");

    fclose(fp);
    fp = NULL;
}
